package com.dinggou.shopjb.activitys;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.dinggou.shopjb.R;
import com.dinggou.shopjb.base.BaseActivity;
import com.dinggou.shopjb.bean.UserInfoBean;
import com.dinggou.shopjb.net.ActionCallbackListener;
import com.dinggou.shopjb.utils.DataUtils;
import com.dinggou.shopjb.utils.TimeCount;
import com.dinggou.shopjb.utils.ToastUtil;
import com.dinggou.shopjb.view.MyEditText;

public class SetPayPwdActivity extends BaseActivity implements View.OnClickListener {
    private MyEditText et_code, met_pwd, met_confirm_pwd;
    private TextView tv_phone, tv_get_code;
    private Button bt_submit;
    private String mobile = "";
    private TimeCount timeCount;
    private static final String PWD_REGEX = "^(?![0-9]+$)(?![a-zA-Z]+$)(?![a-zA-Z\\\\W]+$)(?![0-9\\\\W]+$)[0-9A-Za-z#%&*@，,。.？?：:；;！!+-=+-=]{6,20}$";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    private void initData() {
        UserInfoBean bean = DataUtils.getUserInfo(mContext);
        if (bean == null) return;
        mobile = bean.mobile;
        tv_phone.setText(mobile);
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_set_pay_pwd;
    }

    @Override
    protected void findViews() {
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        et_code = (MyEditText) findViewById(R.id.et_code);
        tv_get_code = (TextView) findViewById(R.id.tv_get_code);
        met_pwd = (MyEditText) findViewById(R.id.met_pwd);
        met_confirm_pwd = (MyEditText) findViewById(R.id.met_confirm_pwd);
        bt_submit = (Button) findViewById(R.id.bt_submit);
    }

    @Override
    protected void setListeners() {
        tv_get_code.setOnClickListener(this);
        bt_submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_get_code:
                getCode();
                break;
            case R.id.bt_submit:
                submit();
                break;
        }
    }

    private void getCode() {
        loading();
        mAppAction.getPayPasswordCode(new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                cancelLoading();
                timeCount = new TimeCount(tv_get_code, 60000, 1000, "再次获取");
                timeCount.start();
                ToastUtil.showToast(mContext, "验证码发送成功");
            }

            @Override
            public void onFailure(String errorCode, String message) {
                cancelLoading();
                ToastUtil.showToast(mContext, message);
            }
        });
    }

    private void submit() {
        if (TextUtils.isEmpty(et_code.getText().toString().trim())) {
            ToastUtil.showToast(mContext, "验证码不能为空");
            return;
        }

        if (TextUtils.isEmpty(met_pwd.getText().toString().trim())) {
            ToastUtil.showToast(mContext, "密码不能为空");
            return;
        }

        if (TextUtils.isEmpty(met_confirm_pwd.getText().toString().trim())) {
            ToastUtil.showToast(mContext, "确认密码不能为空");
            return;
        }
//        Pattern p= Pattern.compile(PWD_REGEX);
//        Matcher m=p.matcher(met_pwd.getText().toString());
//        if (!m.matches()) {
//            ToastUtil.showToast(mContext, "密码需由6-20位字母、数字或符号中的两种及以上组成");
//            return;
//        }
        if (met_pwd.getText().toString().length() < 6){
            ToastUtil.showToast(mContext, "密码长度最少6位");
            return;
        }

        if (!met_confirm_pwd.getText().toString().equals(met_pwd.getText().toString())) {
            ToastUtil.showToast(mContext,"两次输入的密码不一致");
            return;
        }

        loading();
        mAppAction.setPayPassword(et_code.getText().toString(), met_pwd.getText().toString(), met_confirm_pwd.getText().toString(),
                new ActionCallbackListener<Void>() {
                    @Override
                    public void onSuccess(Void data) {
                        cancelLoading();
                        ToastUtil.showToast(mContext, "新密码设置成功");
                        backActivity();
                    }

                    @Override
                    public void onFailure(String errorCode, String message) {
                        cancelLoading();
                        ToastUtil.showToast(mContext, message);
                    }
                });
    }
}